package com.sk.ccflower.server.lib.sysUtils;

import java.io.File;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.sk.ccflower.lib.commonUtils.ConfigUtil;
import com.sk.ccflower.lib.commonUtils.FileUtil;
import com.sk.ccflower.lib.define.BaseCfg;


public class BaseMain
{
	
    protected Logger m_log;


    public BaseMain() throws Exception
    {
        this(60*1000);
    }

    public BaseMain(int interval) throws Exception
    {
        init( interval);
    }

    public void chagngeProperties(String oriFileName , String  destFileName ) throws Exception{

        File ori = new File(oriFileName);
        File dest = new File(destFileName);
        new FileUtil().newFile(destFileName);
        String logPath = Environment.getLogDir() + File.separator + getClass().getSimpleName() + ".log";
        HashMap<String , String> att = new HashMap<String , String>();
        att.put("log4j.appender.CON.File" , logPath );
        ConfigUtil.getInstance().changeCopyProperties(ori , dest , att);
    }

    private void init( int interval) throws Exception
    {
        String log4j = Environment.getCfgLog4jDir() + File.separator + "log4j." + getClass().getSimpleName() + ".properties";
        String log4jRun = Environment.getCfgLog4jRunDir() + File.separator + "log4j." + getClass().getSimpleName() + ".properties";
        this.chagngeProperties(log4j ,log4jRun);
        PropertyConfigurator.configureAndWatch( log4jRun, interval);
        this.m_log = Logger.getLogger(getClass());

        this.m_log.info("*****************************************************************");
        this.m_log.info("* " + getClass().getName() +" STARTUP");
        this.m_log.info("*****************************************************************");
        this.loadMyBaseCfg();

    }

    public void loadMyBaseCfg() throws Exception{
        String baseCfgPath = Environment.getCfgConfigDir() + File.separator + BaseCfg.class.getSimpleName() + ".cfg";
        ConfigUtil.getInstance().readAllProperty(BaseCfg.class, baseCfgPath);
    }



    public void loadMyCfg(Class<?> cls) throws Exception
    {
        String cfgPath = Environment.getCfgConfigDir() + File.separator + getClass().getSimpleName() + ".cfg";
        ConfigUtil.getInstance().readAllProperty(cls, cfgPath);
    }

}
